package com.zrrd.mcl.item.servict.impl;

import com.alibaba.fastjson.JSON;
import com.zrrd.mcl.item.servict.ItemService;
import com.zrrd.mcl.list.client.ListFeignClient;
import com.zrrd.mcl.model.product.BaseCategoryView;
import com.zrrd.mcl.model.product.SkuInfo;
import com.zrrd.mcl.model.product.SpuSaleAttr;
import com.zrrd.mcl.product.client.ProductFeiginClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;

@Service
public class ItemServiceImpl implements ItemService {

    @Autowired
    private ProductFeiginClient productFeiginClient;

    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;

    @Autowired
    private ListFeignClient listFeignClient;

    @Override
    public Map<String, Object> getProductInfo(Long skuId) {
        Map<String, Object> map = new HashMap<>();

        //获取sku基本信息
        CompletableFuture<SkuInfo> skuInfoCompletableFuture = CompletableFuture.supplyAsync(() -> {
            SkuInfo skuInfo = productFeiginClient.getSkuInfo(skuId);
            map.put("skuInfo", skuInfo);
            return skuInfo;
        }, threadPoolExecutor);


        //获取分类信息
        CompletableFuture<Void> categoryViewCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {
            BaseCategoryView categoryView = productFeiginClient.getCategoryView(skuInfo.getCategory3Id());
            map.put("categoryView", categoryView);
        }, threadPoolExecutor);


        //获取价格信息
        CompletableFuture<Void> skuPriceCompletableFuture = CompletableFuture.runAsync(new Runnable() {

            @Override
            public void run() {
                BigDecimal skuPrice = productFeiginClient.getSkuPrice(skuId);
                map.put("price", skuPrice);
            }
        }, threadPoolExecutor);


        //获取销售属性信息
        CompletableFuture<Void> SpuSaleAttrCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {
            List<SpuSaleAttr> spuSaleAttrs = productFeiginClient.getSpuSaleAttrListBySku(skuInfo.getSpuId(), skuId);
            map.put("spuSaleAttrList", spuSaleAttrs);
        }, threadPoolExecutor);


        //获取切换商品的JSON数据
        CompletableFuture<Void> valueMapCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {
            Map valueMap = productFeiginClient.getSpuSaleAttrListBySku(skuInfo.getSpuId());
            String valuesJson = JSON.toJSONString(valueMap);
            map.put("valuesSkuJson", valuesJson);
        }, threadPoolExecutor);

        //更新商品热度
        CompletableFuture<Void> voidCompletableFuture = CompletableFuture.runAsync(() -> {
            listFeignClient.updateHotScore(skuId);
        }, threadPoolExecutor);

        //创建多任务组合并且执行
        CompletableFuture.allOf(
                skuInfoCompletableFuture,
                categoryViewCompletableFuture,
                skuPriceCompletableFuture,
                SpuSaleAttrCompletableFuture,
                valueMapCompletableFuture,
                voidCompletableFuture
        ).join();

        //返回Map集合

        return map;
    }
}
